/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Main;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import java.sql.Date;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author
 */
public class DiemThi {
    //Danh sach thuoc tinh

    private int id;
    private String userName;
    private int maDeThi;
    private int diem;
    private Date ngayThi;

    //Ham get va set
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public int getMaDeThi() {
        return maDeThi;
    }

    public void setMaDeThi(int maDeThi) {
        this.maDeThi = maDeThi;
    }

    public float getDiem() {
        return diem;
    }

    public void setDiem(int diem) {
        this.diem = diem;
    }

    public Date getNgayThi() {
        return ngayThi;
    }

    public void setNgayThi(Date ngayThi) {
        this.ngayThi = ngayThi;
    }

    //Ham khoi tao
    public DiemThi() {
    }

    public DiemThi(int id, String userName, int maDeThi, int diem, Date ngayThi) {
        this.id = id;
        this.userName = userName;
        this.maDeThi = maDeThi;
        this.diem = diem;
        this.ngayThi = ngayThi;
    }

    //ham lay danh sach diem thi theo username
    public static List<DiemThi> layDSDiemThiTheoUsername(String userName) {
        List<DiemThi> danhSach = new ArrayList<DiemThi>();
        String sql = "select * from diem_thi where user_name='"+userName+"'";
        try {
            //Tao bien ket noi
            Connection connection = KetNoiCSDL.open();
            // Tao bien statement de truy van
            PreparedStatement statement = (PreparedStatement) connection.prepareStatement(sql);
            //Tra ve result set sau khi truy van du lieu
            ResultSet rs = statement.executeQuery();
            //Lap lay tung dong du lieu
            while (rs.next()) {

                DiemThi dt = new DiemThi();
                dt.id=rs.getInt("id");
                dt.maDeThi=rs.getInt("ma_de_thi");
                dt.userName=rs.getString("user_name");
                dt.diem=rs.getInt("diem");
                dt.ngayThi=rs.getDate("ngay_thi");
                danhSach.add(dt);

            }
            connection.close();
        } catch (Exception ex) {
            ex.printStackTrace();
            KetNoiCSDL.close();
        } finally {
            KetNoiCSDL.close();
        }
        return danhSach;
    }
    
    //Ham them diem thi
    public boolean themDiemThi(){
         boolean rs=false;
        String sql = "insert into diem_thi(user_name,ma_de_thi,diem,ngay_thi) values ";
        sql+=" ('"+this.userName+"',"+this.maDeThi+","+this.diem+",'"+this.ngayThi+"')";
        try {
            //Tao bien ket noi
            Connection connection = KetNoiCSDL.open();
            // Tao bien statement de truy van
            PreparedStatement statement = (PreparedStatement) connection.prepareStatement(sql);
            //Tra ve resulrs = true;t set sau khi truy van du lieu
            
            if(statement.executeUpdate() == 1){
                rs=true;
            }

            connection.close();
        } catch (Exception ex) {
            ex.printStackTrace();
            KetNoiCSDL.close();
        } finally {
            KetNoiCSDL.close();
        }

        return rs;
    }
}
